import pygame, math

class health_bar:
    def __init__(self, game):
        self.game = game
        self.game_logic = game.game_logic
        
        self.image = pygame.Surface((self.game_logic.screen_rect.w, 10))
        self.rect = self.image.get_rect()
        self.rect.bottom = self.game_logic.screen_rect.bottom
        
        self.image.fill((255, 0, 0))
        
        self.health = 100
        self.alive = True
        self.dirty = True
    
    def update(self):
        if self.dirty:
            ratio = float(self.health) / 100.0
            width = self.game_logic.screen_rect.w * ratio
            red = 255 - int(math.ceil(255.0 * ratio))
            blue = int(math.ceil(255.0 * ratio))
            color = (red, 0, blue)
            self.image = pygame.Surface((width,10))
            self.image.fill(color)
            self.rect = self.image.get_rect()
            self.rect.left = 0
            self.rect.bottom = self.game_logic.screen_rect.bottom
            self.dirty = False
    
    def set_health(self, h):
        if h >= 0 and h <= 100:
            self.health = h
        else:
            if h < 0:
                self.h = 0
            else:
                self.h = 100
        self.dirty = True
